///////////////////////////////////////////////////////
//	Game: Wings of Death
//	Team: DeltaMu
//	Members:	Matt Campbell
//				Nicolas Ploetz
//				Jon Leopard
//	Date: 12/6/13
///////////////////////////////////////////////////////


#include <windows.h>	
#include <tchar.h>
#include <vld.h>		

#include "Game.h"

const TCHAR*	g_szWINDOW_CLASS_NAME	= _T("SGP Wings of Death");		
const TCHAR*	g_szWINDOW_TITLE		= _T("SGP Wings of Death");		
const int		g_nWINDOW_WIDTH			= 1024;							
const int		g_nWINDOW_HEIGHT		= 640;							
const BOOL		g_bIS_WINDOWED			= TRUE;							


BOOL IsAlreadyRunning( void );
BOOL RegisterWindowClass( HINSTANCE hInstance );
HWND MakeWindow( HINSTANCE hInstance );
LRESULT CALLBACK WindowProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );




int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG		msg;	
	HWND	hWnd;	

	if( hPrevInstance == 0 )
	{
		if( IsAlreadyRunning() == TRUE )
			return -1;
	}

	if( RegisterWindowClass( hInstance ) == FALSE )
	{
		MessageBox( HWND_DESKTOP, _T("ERROR: Failed to register the window class."),
			g_szWINDOW_TITLE, MB_OK | MB_ICONERROR );

		return -2;
	}

	hWnd = MakeWindow(hInstance);

	if( hWnd == 0 )
	{
		MessageBox( HWND_DESKTOP, _T("ERROR: Failed to create the Main Window."),
			g_szWINDOW_TITLE, MB_OK | MB_ICONERROR );
		
		UnregisterClass( g_szWINDOW_CLASS_NAME, hInstance );

		return -3;
	}

	ShowWindow( hWnd, nCmdShow );
	UpdateWindow( hWnd );

	CGame* pGame = CGame::GetInstance();

	pGame->Initialize(  hWnd, hInstance, 
						g_nWINDOW_WIDTH, g_nWINDOW_HEIGHT,
						g_bIS_WINDOWED );
					
	while( true )
	{
		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) == TRUE )
		{ 
			if( msg.message == WM_QUIT )
				break;
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{
			if( pGame->Main() == false )
				PostQuitMessage( 0 );
		}
	}

	pGame->Terminate();
	CGame::DeleteInstance();
	pGame = nullptr;

	UnregisterClass( g_szWINDOW_CLASS_NAME, hInstance );
	
	return (int)(msg.wParam);
}


BOOL IsAlreadyRunning(void)
{
	HWND hOtherWnd = FindWindow( g_szWINDOW_CLASS_NAME, g_szWINDOW_TITLE );

	if( hOtherWnd )
	{
		if( IsIconic( hOtherWnd ) == TRUE )
			ShowWindow( hOtherWnd, SW_RESTORE );
		SetForegroundWindow( hOtherWnd );
		return TRUE;
	}
	return FALSE;
}

BOOL RegisterWindowClass(HINSTANCE hInstance)
{
	WNDCLASSEX	winClassEx;	

	winClassEx.cbSize			= sizeof(winClassEx);
	winClassEx.style			= CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	winClassEx.lpfnWndProc		= WindowProc;
	winClassEx.cbClsExtra		= 0;
	winClassEx.cbWndExtra		= 0;
	winClassEx.hInstance		= hInstance;
	winClassEx.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	winClassEx.hIconSm			= NULL;
	winClassEx.hCursor			= LoadCursor(NULL, IDC_ARROW);
	winClassEx.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	winClassEx.lpszMenuName		= NULL; 
	winClassEx.lpszClassName	= g_szWINDOW_CLASS_NAME;

	return RegisterClassEx( &winClassEx );
}

HWND MakeWindow(HINSTANCE hInstance)
{
	DWORD dwWindowStyleFlags = WS_VISIBLE | WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;

	RECT rWindow;
	rWindow.left	= 0;
	rWindow.top		= 0;
	rWindow.right	= g_nWINDOW_WIDTH;
	rWindow.bottom	= g_nWINDOW_HEIGHT;

	AdjustWindowRectEx( &rWindow, 
						dwWindowStyleFlags,
						FALSE, 
						WS_EX_APPWINDOW );
	
	int nWindowWidth	= rWindow.right - rWindow.left;
	int nWindowHeight	= rWindow.bottom - rWindow.top;

	return CreateWindowEx(WS_EX_APPWINDOW,											
						  g_szWINDOW_CLASS_NAME,									
						  g_szWINDOW_TITLE,											
						  dwWindowStyleFlags,										
						  (GetSystemMetrics(SM_CXSCREEN) - nWindowWidth)/2,			
						  (GetSystemMetrics(SM_CYSCREEN) - nWindowHeight)/2,		
						  nWindowWidth,												
						  nWindowHeight,											
						  HWND_DESKTOP,												
						  NULL,														
						  hInstance,												
						  NULL);													
}

LRESULT CALLBACK WindowProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
	
	case WM_CREATE: 
		{
		}
		break;
	

	case WM_DESTROY: 
		{
			PostQuitMessage( 0 );
		}
		break;


	case WM_SYSKEYUP:
	case WM_SYSCHAR:
		{
		}
		break;


	case WM_CLOSE:
		{
			DestroyWindow(hWnd);			
		}
		break;


	case WM_ACTIVATE:
		{
			if( LOWORD( wParam ) != WA_INACTIVE )	
			{
				// Unpause game here
			}
			else									
			{
				// Pause game here
			}
		}
		break;

	
	case WM_PAINT:
		{
			PAINTSTRUCT	ps;
			HDC			hdc;

			hdc = BeginPaint( hWnd,&ps );

			EndPaint( hWnd,&ps );
		}
		break;

	
	default:
		return DefWindowProc( hWnd, msg, wParam, lParam );
	}

	return 0;
}
